<?php



/**
 * User
 */
class User
{
    /**
     * @var integer $id
     */
    private $id;

    /**
     * @var string $name
     */
    private $name;

    /**
     * @var string $email
     */
    private $email;

    /**
     * @var string $salt
     */
    private $salt;

    /**
     * @var string $password
     */
    private $password;

    /**
     * @var string $avatar
     */
    private $avatar;

    /**
     * @var boolean $active
     */
    private $active;

    /**
     * @var datetime $created_at
     */
    private $created_at;

    /**
     * @var datetime $connected_at
     */
    private $connected_at;

    /**
     * @var Group
     */
    private $groups;

    public function __construct()
    {
        $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Get name
     *
     * @return string $name
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set email
     *
     * @param string $email
     */
    public function setEmail($email)
    {
        $this->email = $email;
    }

    /**
     * Get email
     *
     * @return string $email
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set salt
     *
     * @param string $salt
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;
    }

    /**
     * Get salt
     *
     * @return string $salt
     */
    public function getSalt()
    {
        return $this->salt;
    }

    /**
     * Set password
     *
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->password = $password;
    }

    /**
     * Get password
     *
     * @return string $password
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set avatar
     *
     * @param string $avatar
     */
    public function setAvatar($avatar)
    {
        $this->avatar = $avatar;
    }

    /**
     * Get avatar
     *
     * @return string $avatar
     */
    public function getAvatar()
    {
        return $this->avatar;
    }

    /**
     * Set active
     *
     * @param boolean $active
     */
    public function setActive($active)
    {
        $this->active = $active;
    }

    /**
     * Get active
     *
     * @return boolean $active
     */
    public function getActive()
    {
        return $this->active;
    }

    /**
     * Set created_at
     *
     * @param datetime $createdAt
     */
    public function setCreatedAt($createdAt)
    {
        $this->created_at = $createdAt;
    }

    /**
     * Get created_at
     *
     * @return datetime $createdAt
     */
    public function getCreatedAt()
    {
        return $this->created_at;
    }

    /**
     * Set connected_at
     *
     * @param datetime $connectedAt
     */
    public function setConnectedAt($connectedAt)
    {
        $this->connected_at = $connectedAt;
    }

    /**
     * Get connected_at
     *
     * @return datetime $connectedAt
     */
    public function getConnectedAt()
    {
        return $this->connected_at;
    }

    /**
     * Add groups
     *
     * @param Group $groups
     */
    public function addGroups(\Group $groups)
    {
        $this->groups[] = $groups;
    }

    /**
     * Get groups
     *
     * @return Doctrine\Common\Collections\Collection $groups
     */
    public function getGroups()
    {
        return $this->groups;
    }

    /**
     * Droits de l'utilisateur
     * @return \Doctrine\Common\Collections\ArrayCollection Collection de droits
     */
    private $credentials;
    public function getCredentials() {
        if (is_null($this->credentials)) {
            $credentials = array();
            foreach ($this->groups AS $group) {
                $credentials = array_merge($credentials, $group->getCredentials()->toArray());
            }
            $this->credentials = new \Doctrine\Common\Collections\ArrayCollection($credentials);
        }
        return $this->credentials;
    }

    /*
     * Retourne la couleur du profil
     * @return <string> Couleur
     */
    public function getColor() {
        return ($this->groups->isEmpty()) ? null : $this->groups->first()->getcolor();
    }

    /*
     * Affichage sous forme de lien
     */
    public function getLink() {
        $color = ( $this->groups->isEmpty() ) ?
            '' :
            'style="color: '.$this->getColor().'"';
        return '<a class="user-link" '.$color.' href="' . url_for('user', 'profil', null, array($this->getId()) ) . '">'.$this->getName().'</a>';
    }

    /**
     * Affiche l'avatar
     * @param <boolean> $_min Miniature ?
     */
    public function renderAvatar($_min = false) {
        echo '<a href="'.url_for('user', 'profil', null, array($this->getId())).'" class="'.( $_min ? 'min-avatar' : 'avatar').'"><img src="'.(empty($this->avatar) ? \cass\Data::directories('upload_avatars') . 'default.png' : $this->avatar).'" alt="Avatar de " /></a>';
    }

}